package pod

import (
	"context"
	"fmt"
	"go-pod/api/internal/svc"
	"go-pod/api/internal/types"
	"go-pod/pod-rpc/pod"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetPodcastLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetPodcastLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPodcastLogic {
	return &GetPodcastLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetPodcastLogic) GetPodcast(req *types.PodcastReq) (resp *types.PodcastResp, err error) {
	// 参数校验
	if req.Id == 0 {
		return nil, fmt.Errorf("播客ID不能为空")
	}

	// 调用RPC服务获取播客详情
	rpcResp, err := l.svcCtx.PodRpcClient.GetPodcast(l.ctx, &pod.GetPodcastReq{
		Id: req.Id,
	})
	if err != nil {
		l.Logger.Errorf("获取播客详情失败: %v", err)
		return nil, fmt.Errorf("获取播客详情失败，请稍后重试")
	}

	// 构建响应
	resp = &types.PodcastResp{
		Id:         req.Id,
		Title:      rpcResp.Title,
		Content:    rpcResp.Content,
		Author:     rpcResp.Author,
		Created_at: rpcResp.CreatedAt,
		Updated_at: rpcResp.UpdatedAt,
	}

	return resp, nil
}
